//
//  TopLoadingView.m
//  MotoControl
//
//  Created by xiongfeng on 14-11-10.
//  Copyright (c) 2014年 xiongfeng. All rights reserved.
//

#import "TopLoadingView.h"

#define BGWIDTH 100.f
#define BGHEIGHT 110.f
#define CENTERWIDTH 35.f
#define TAGCONTAINER 11111
#define TAGCIRCLE 22222
#define TAGLABEL 33333

static TopLoadingView *lv = nil;
static id<CancelLoadingDelegate> delegate = nil;
@implementation TopLoadingView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+ (void)showInView:(UIView*)view title:(NSString*)title delegate:(id<CancelLoadingDelegate>)delegate_ {
    [TopLoadingView showInView:view title:title delegate:delegate_ obscure:YES];
}

+ (void)showInView:(UIView*)view title:(NSString*)title delegate:(id<CancelLoadingDelegate>)delegate_ obscure:(BOOL)obs {
    [self showInView:view title:title delegate:delegate_ obscure:obs useClose:YES];
}

+ (void)showInView:(UIView*)view title:(NSString*)title delegate:(id<CancelLoadingDelegate>)delegate_ obscure:(BOOL)obs useClose:(BOOL)useClose {
    if (view==nil) {
        return;
    }
    [TopLoadingView hide];
    
    if (lv==nil) {
        lv = [[TopLoadingView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds))];
        if (obs) {
            lv.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        } else {
            lv.backgroundColor = [UIColor clearColor];
        }
    }
    
    if ([lv superview]) {
        [lv removeFromSuperview];
    }
    
    if ([lv subviews].count==0) {
        UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BGWIDTH, BGHEIGHT)];
        container.layer.cornerRadius = 8.f;
        container.backgroundColor = [UIColor whiteColor];
        container.center = view.center;
        container.tag = TAGCONTAINER;
//        UIImageView *loadingImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, BGWIDTH, BGHEIGHT)];
//        [loadingImg setImage:IMAGE(@"bg_loadingview")];
//        [container addSubview:loadingImg];
        
        UIImageView *ivCircle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loadingcircle"]];
        ivCircle.center = CGPointMake(CGRectGetMidX(container.bounds), CGRectGetMidY(container.bounds) - 10.f);
        ivCircle.tag = TAGCIRCLE;
        [container addSubview:ivCircle];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BGWIDTH, 20.f)];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont systemFontOfSize:14.f];
        label.textColor = RGBColor(145, 145, 145);
        label.text = title;
        label.center = CGPointMake(CGRectGetMidX(container.bounds), CGRectGetMidY(container.bounds)+25.f);
        label.tag = TAGLABEL;
        [container addSubview:label];
        
        if (useClose) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setFrame:CGRectMake(0, 0, 25, 25)];
            [btn setImage:[UIImage imageNamed:@"btn_close_loading"] forState:UIControlStateNormal];
            btn.center = CGPointMake(CGRectGetMaxX(container.bounds)-15.f, CGRectGetMinY(container.bounds)+15.f);
            [btn addTarget:lv action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [container addSubview:btn];
        }
        
        [lv addSubview:container];
        
        delegate = delegate_;
        
    }
    
    [view addSubview:lv];
    [view bringSubviewToFront:lv];
    
    UIView *container = [lv viewWithTag:TAGCONTAINER];
    UIImageView *circle = (UIImageView*)[container viewWithTag:TAGCIRCLE];
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: 2*M_PI];
    rotationAnimation.duration = 0.5;
    rotationAnimation.repeatCount = MAXFLOAT;
    rotationAnimation.cumulative = YES;
    rotationAnimation.removedOnCompletion = NO;
    [circle.layer addAnimation:rotationAnimation forKey:@"rotationAnimation1"];
}

+ (void)hide {
    if (lv) {
        UIView *container = [lv viewWithTag:TAGCONTAINER];
        UIImageView *circle = (UIImageView*)[container viewWithTag:TAGCIRCLE];
        if (circle) {
            [circle.layer removeAllAnimations];
        }
        
        if ([lv superview]) {
            [lv removeFromSuperview];
        }
        
        delegate = nil;
        lv = nil;
    }
}

- (void)btnClicked:(id)sender {
    if (delegate&&[delegate respondsToSelector:@selector(cancelLoading)]){
        [delegate cancelLoading];
    }
    
    [TopLoadingView hide];
}

@end
